Skip to content

Conversation

@MasterOdin
Copy link
Contributor

@MasterOdin MasterOdin commented Mar 15, 2025

Closes #169

Adds a promise return for getSocket as well as getSocketPromise function, so as to match the getPort and getPorts functions. Also adds type information for these functions as that was totally missing before.

The overall tests that are run remain the same, just that they're run against all three variants as was setup in #172.

@MasterOdin MasterOdin force-pushed the feat-socket-promise branch 2 times, most recently from 7e77b11 to a15c15a Compare March 15, 2025 03:58
Comment on lines -92 to -96
portfinder.getSocket({
path: path.join(badDir, 'test.sock'),
}, function () {
done();
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling getSocket here was probably not meaningfully necessary to waiting for createServers to finish, so I removed it in favor of just calling done.

Comment on lines 56 to 59
if (process.platform === 'win32') {
fs.unlinkSync(sock);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stopServers function meaning shouldn't have changed with the move to async.each beyond just being more concise, though this block is new, and necessary for cleaning up sockets on windows due to the above symlink shenanigans.

});
} else {
return internals.getPorts(count, options, callback);
internals.getPorts(count, options, callback);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the change to internals.getPort above are no-ops in terms of what they do (as the functions don't return anything), but I feel this is more "semantically correct" in that we don't expect this function to return anything if not a promise.

@MasterOdin MasterOdin force-pushed the feat-socket-promise branch 2 times, most recently from 5ec34aa to ecffd0c Compare March 15, 2025 04:08
Signed-off-by: Matthew Peveler <[email protected]>
@MasterOdin MasterOdin force-pushed the feat-socket-promise branch from ecffd0c to f4e9d55 Compare March 15, 2025 04:13
Comment on lines +186 to +188
// We don't use `test.sock` here due to some race condition on Windows in freeing the `test.sock` file
// when we close the servers.
test('with a directory that exists should respond with the first free socket (foo.sock)', function (done) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was mentioning test.sock, but the code was using exists.sock. I'm guessing this was due to an issue on windows where the socket files were not being cleared as part of cleanup. While this was fixed, I think there's still some weird race condition where calling this function too quickly will still fail on Windows if using test.sock.

As such, I went with calling it foo.sock to avoid the name collision and that I renamed it as I didn't like calling it exists.sock as that felt like it was implying the socket file already existed.

name = base === 0 ? 'test.sock' : 'test' + base + '.sock';
let sock = path.join(socketDir, name);
const socket = path.join(socketDir, name);
let sock = socket;
Copy link
Member

@eriktrom eriktrom Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just have 1 variable socket? Or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the platform is windows, we append the pipe stuff to the front of sock to be passed to server.listen. However, we create the file without the pipe stuff, so that's why I've used the two separate variables, socket to track the filename as generated, and then sock to be modified with the pipe stuff.

Copy link
Member

@eriktrom eriktrom Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

@eriktrom
Copy link
Member

I cleaned up my comments. I see windows is a little feisty :)

Just one question about variable naming. The rest all makes sense.

This is awesome work, thank you very much, I really appreciate it 👍

@eriktrom
Copy link
Member

eriktrom commented Apr 2, 2025

Thanks again @MasterOdin

@eriktrom eriktrom merged commit 0f01a6f into http-party:master Apr 2, 2025
19 checks passed
@MasterOdin MasterOdin deleted the feat-socket-promise branch April 2, 2025 05:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add promise variant of getSocket method

2 participants